// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-16 01:01
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

public class Merge {
    /**
     * 数组的归并排序 ->
     */
    public void mergeT(int[] nums1, int m, int[] nums2, int n) {
        if (m + n - m >= 0) {
            System.arraycopy(nums2, m - m, nums1, m, m + n - m);
        }
        Arrays.sort(nums1);
    }

    public void merge(int[] nums1, int m, int[] nums2, int n) {
        if (n == 0) {
            return;
        }
        if (m == 0) {
            if (n >= 0) {
                System.arraycopy(nums2, 0, nums1, 0, n);
            }
        }
        int[] tmp = new int[m];
        System.arraycopy(nums1, 0, tmp, 0, m);
        int p = 0, q = 0, k = 0;
        while (p < m && q < n) {
            if (tmp[p] < nums2[q]) {
                nums1[k++] = tmp[p++];
            } else {
                nums1[k++] = nums2[q++];
            }
        }
        while (q < n) {
            nums1[k++] = nums2[q++];
        }
        while (p < m) {
            nums1[k++] = tmp[p++];
        }
    }

    @Test
    public void shout() {
        int[] nums1 = {2, 0}, nums2 = {1};
        int m = 1, n = 1;
        merge(nums1, m, nums2, n);
        for (int i = 0; i < nums1.length; i++) {
            System.out.print(nums1[i] + "\t");
        }
    }

}
